package com.etc.cure.service.Impl;

import cn.hutool.json.JSONUtil;

import com.etc.common.mapper.LogMapper;
import com.etc.common.utils.Result;
import com.etc.common.utils.UUIDUtil;
import com.etc.cure.R.ROperation;
import com.etc.cure.entity.Operation;
import com.etc.cure.mapper.OperationMapper;
import com.etc.cure.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.etc.common.utils.RedisConstants.CACHE_OPERATION;

@Service
public class OperationServiceImpl implements OperationService {

    @Autowired
    private OperationMapper operationMapper;



    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private LogMapper logMapper;

    /**
     * 新增手术
     * @param operation
     * @return
     */
    @Override
    @Transactional
    public Result insert(Operation operation,String operatorId) {
        //设置手术id
        operation.setOperationId(UUIDUtil.getUUID());
        //新增手术
        int result = operationMapper.insert(operation);
        if (result>0){
            //删除redis(删除执行手术的医生的redis缓存)
            stringRedisTemplate.delete(CACHE_OPERATION+":docId:"+operation.getDocId());
            //新增日志
            logMapper.insert(UUIDUtil.getUUID(),operatorId,"新增手术："+operation.getOperationId());
            return Result.ok(1,"新增手术成功！");
        }
        return Result.fail(-1,"新增手术失败！");
    }


    /**
     * 通过医生id获取手术列表
     * @param docId
     * @return
     */
    @Override
    public Result queryAllByDocId(Integer pageNum,Integer pageSize,String docId){
        //获取是否有值
        Boolean isExist = stringRedisTemplate.hasKey(CACHE_OPERATION+":docId:"+docId);
        if (!isExist){
            //没有数据，则查询数据库
            List<ROperation> list = operationMapper.queryAllByDocId(docId);
            for (ROperation rOperation:list){
                //转化成json
                String jsonStr = JSONUtil.toJsonStr(rOperation);
                //写入redis
                stringRedisTemplate.opsForList().leftPush(CACHE_OPERATION+":docId:"+docId,jsonStr);
            }
        }
        //有值，从redis中获取手术数据
        List<String> result = stringRedisTemplate.opsForList().range(CACHE_OPERATION+":docId:"+docId,
                pageSize*(pageNum-1), pageSize*(pageNum-1)+pageSize-1);
        //转化成list
        List<ROperation> list = JSONUtil.toList(result.toString(), ROperation.class);
        return Result.ok(list);
    }

    /**
     * 根据门诊Id查询手术信息
     * @param osId
     * @return
     */
    @Override
    public Result queryByOsId(String osId) {
        return Result.ok(operationMapper.queryByOsId(osId));
    }

    /**
     * 根据手术编号修改手术详情
     * @param operation
     * @param operatorId
     * @return
     */
    @Override
    @Transactional
    public Result update(Operation operation, String operatorId){
        //修改数据库
        operationMapper.update(operation);
        //删除redis(删除执行手术的医生的redis缓存)
        stringRedisTemplate.delete(CACHE_OPERATION+":docId:"+operation.getDocId());
        //添加日志
        logMapper.insert(UUIDUtil.getUUID(), operatorId,
                "修改手术详情的信息ID为：" + operation.getOperationId());
        return Result.ok(1,"修改成功");
    }

    @Override
    public Result queryOpDoc() {
        return Result.ok(operationMapper.queryOpDoc());
    }
}
